package com.mashibing.tank;

import com.mashibing.tank.strategy.FireStrategy;
import lombok.Data;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.util.UUID;

/**
 * @description:
 * @author: 余希瑶
 * @date: 2021年06月18日 10:20
 * @version:1.0
 */
@Data
public class Player extends AbstractGameObject {
    public static int WIDTH = ResourceMgr.goodTankU.getWidth();

    public static int HEIGHT = ResourceMgr.goodTankU.getHeight();

    private UUID id = UUID.randomUUID();

    //记录键盘是否被按下的变量
    public static final int SPEED = 5;
    private int x, y;
    private Dir dir;
    private boolean bL, bU, bR, bD;
    //是否移动
    private boolean moving = false;
    private Group group;
    private boolean live = true;

    FireStrategy strategy = null;

    public Player(int x, int y, Dir dir, Group group) {
        this.x = x;
        this.y = y;
        this.dir = dir;
        this.group = group;

        //init fire strategy from config file
        this.initFireStrategy();
    }

    public void paint(Graphics g) {

        if (!this.isLive()) {
            return;
        }

        Color c =g.getColor();
        g.setColor(Color.yellow);
        g.drawString(id.toString(),x,y-10);
        g.setColor(c);

        switch (dir) {
            case L:
                g.drawImage(ResourceMgr.goodTankL, x, y, null);
                break;
            case U:
                g.drawImage(ResourceMgr.goodTankU, x, y, null);
                break;
            case R:
                g.drawImage(ResourceMgr.goodTankR, x, y, null);
                break;
            case D:
                g.drawImage(ResourceMgr.goodTankD, x, y, null);
                break;
        }


        if (this.group == Group.BAD) {
            switch (dir) {
                case L:
                    g.drawImage(ResourceMgr.badTankL, x, y, null);
                    break;
                case U:
                    g.drawImage(ResourceMgr.badTankU, x, y, null);
                    break;
                case R:
                    g.drawImage(ResourceMgr.badTankR, x, y, null);
                    break;
                case D:
                    g.drawImage(ResourceMgr.badTankD, x, y, null);
                    break;
            }
        }

        move();
    }

    public void keyPressed(KeyEvent e) {
        int key = e.getKeyCode();
        switch (key) {
            case KeyEvent.VK_LEFT:
                //dir = Dir.L;
                //x -= SPEED;
                bL = true;
                break;
            case KeyEvent.VK_UP:
                //dir = Dir.U;
                //y -= SPEED;
                bU = true;
                break;
            case KeyEvent.VK_RIGHT:
                //dir = Dir.R;
                //x += SPEED;
                bR = true;
                break;
            case KeyEvent.VK_DOWN:
                bD = true;
                break;

        }
        //设置方向
        setMainDir();

    }

    private void setMainDir() {
        //all dir keys are released, tank shouId be stop


        if (!bL && !bU && !bR && !bD) {
            moving = false;
        } else {
            //any dir key is pressed, tank should be moving
            moving = true;
            if (bL && !bU && !bR && !bD) {
                dir = Dir.L;
            }
            if (!bL && bU && !bR && !bD) {
                dir = Dir.U;
            }
            if (!bL && !bU && bR && !bD) {
                dir = Dir.R;
            }
            if (!bL && !bU && !bR && bD) {
                dir = Dir.D;
            }

            /*// 左上
            if (bL && bU && !bR && !bD) {
                dir =Dir.LU;
            }
            // 左下
            if (bL && !bU && !bR && bD) {
                dir =Dir.LD;
            }
            // 右上
            if (!bL && !bU && bR && bD) {
                dir =Dir.RU;
            }
            // 右下
            if (!bL && bU && bR && !bD) {
                dir =Dir.RD;
            }*/
        }

    }

    private void move() {
        if (!moving) return;
        switch (dir) {
            case L:
                x -= SPEED;
                break;
            case U:
                y -= SPEED;
                break;
            case R:
                x += SPEED;
                break;
            case D:
                y += SPEED;
                break;
            //左上
          /*  case LU:
                x -= SPEED;
                y -= SPEED;
                break;
            //左下
            case LD:
                x -= SPEED;
                y += SPEED;
                break;
            //右上
            case RU:
                x += SPEED;
                y += SPEED;
                break;
            //右下
            case RD:
                x += SPEED;
                y -= SPEED;
                break;*/
        }
    }

    public void keyReleased(KeyEvent e) {
        int key = e.getKeyCode();
        switch (key) {
            case KeyEvent.VK_LEFT:
                bL = false;
                break;
            case KeyEvent.VK_UP:
                bU = false;
                break;
            case KeyEvent.VK_RIGHT:
                bR = false;
                break;
            case KeyEvent.VK_DOWN:
                bD = false;
                break;
            case KeyEvent.VK_CONTROL:
                fire();
                break;
        }

        setMainDir();
    }


    private void initFireStrategy() {

        String className = PropertyMgr.get("tankFireStrategy");
        try {
            Class<?> clazz = Class.forName("com.mashibing.tank.strategy." + className);
            strategy = (FireStrategy) clazz.getDeclaredConstructor().newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private void fire() {

        strategy.fire(this);


        //TankFrame.INSTANCE.getBullets().add(b);
    }

    public void die() {
        this.setLive(false);
    }
}
